layui.use(['form', 'layer'], function(){
    var form = layui.form;
    var layer = layui.layer;
    //登陆成功回调
    var loginSuccess = function(data){
        //console.log(data)
        //登陆成功 存储登录凭据
        var kplus_ad_logined_info = {
            aId: data.data.aId,
            aToken: data.data.aToken 
        }
        Cookies.set('kplus_ad_logined_info', kplus_ad_logined_info, {path: WEB_SETTING_OPTIONS.cookies_save_path})
        //存储用户信息
        var kplus_ad_use_info = {
            aName: data.data.aName 
        }
        Cookies.set('kplus_ad_use_info', kplus_ad_use_info, {path: WEB_SETTING_OPTIONS.cookies_save_path})
        //提示跳转
        layer.msg(MESSAGES.login_success, {time: 2000, end: function(){
            location.href = './index.html';
        }})
    }
    var loginFromAjax = function(data){
        var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

        $.ajax({
            url: BASE_SERVICEURL + URlS.admin,
            type: 'POST',
            data: {
                'aMail': data.field.username,
                'aPwd': hex_md5(data.field.password)
            },
            success: function(responseData){
                layer.close(loadIndex);
                if(responseData.msg.code == 200){
                    loginSuccess(responseData)
                }else{
                    layer.msg(MESSAGES.info_checkout_failure + responseData.msg.msg, {time: 2000})
                }
                
            },
            error: function(responseData){
                layer.msg(MESSAGES.service_error, {time: 2000})
            }
        })
    }
    //监听提交
    form.on('submit(login)', function(data){ loginFromAjax(data); }) 
    //enter 提交
    document.onkeydown = function(event){ 
        e = event ? event :(window.event ? window.event : null); 
        //enter 触发
        if(e.keyCode==13){ 
            var loginData = {
                'field':{
                    username: $('input[name="username"]').val(),
                    password: $('input[name="password"]').val(),
                }
            };
            //执行登录
            loginFromAjax(loginData); 
        } 
    } 
});